import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse

from scaner.core.config.project_config import settings
from scaner.core.config.log_config import MyLogger
from scaner.api_v1 import router as router_v1


logger = MyLogger()
logger.info("Start logging")  # type: ignore

favicon_path = "favicon.ico"


def get_application() -> FastAPI:
    application = FastAPI(
        title=settings.project_name,
        debug=settings.debug,
        version=settings.version,
    )

    application.include_router(router_v1, prefix=settings.api_v1_prefix)

    application.add_middleware(
        CORSMiddleware,
        allow_origins=settings.cors_allowed_origins,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    return application


app = get_application()


@app.get("/")
def index():
    logger.info("test test")  # type: ignore
    return {"massage": "It's working!!!"}


@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
    return FileResponse(favicon_path)


if __name__ == "__main__":
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
    )
